
/**
 * User domain class.
 */
class User {
	static transients = ['pass']
	static hasMany = [authorities: Role, events: Event, feedInfos:FeedInfo, emailAddresses: EmailAddress]
	static belongsTo = Role

  static mapping = {
    sort "username"
    cache true
    profile cache:true
  }

  static final String DAILY_DIGEST = 'Daily'
  static final String WEEKLY_DIGEST = 'Weekly'

	String username
	String passwd
  Profile profile
	Boolean enabled = true
  Boolean eventAlert = true
  Boolean dailyDigest = true
  Boolean weeklyDigest = false
	String pass = '[secret]'

	static constraints = {
		username(blank: false, unique: true)
		passwd(blank: false, validator: {
       val, obj ->
          obj.properties['pass'] == val
    })
    profile (nullable: true)
    enabled(nullable: false)
    eventAlert (nullable: false)
    dailyDigest (nullable: false)
    weeklyDigest (nullable: false)
	}

  def afterUpdate = {
    if (id != null) {
      new Message(user: this, content: "$username hat sein Benutzerprofil aktualisiert.", medium: "MoTS Update Notifier").save()
      log.info "User profile of user $username updated"
    }
  }

  public String toString() {
    return username
  }
}
